﻿using Beginor.Owin.StaticFile;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Owin;
using System;
using System.Collections.Generic;
using System.Configuration;

[assembly: OwinStartup(typeof(AisinGioro.Server.OwinStartup))]
namespace AisinGioro.Server
{
    public class OwinStartup
    {
        public void Configuration(IAppBuilder app)
        {
            //静态文件托管
            app.Map("/wwwroot", map =>
            {
                map.UseCors(CorsOptions.AllowAll);
                map.UseStaticFile(new StaticFileMiddlewareOptions
                {
                    RootDirectory = ServerManager.WebRootDirectory,
                    DefaultFile = "index.html",
                    EnableETag = true,
                    EnableHtml5LocationMode = true,
                    MimeTypeProvider = new MimeTypeProvider(new Dictionary<string, string>
                    {
                        { ".html", "text/html" },
                        { ".htm", "text/html" },
                        { ".dtd", "text/xml" },
                        { ".xml", "text/xml" },
                        { ".ico", "image/x-icon" },
                        { ".css", "text/css" },
                        { ".js", "application/javascript" },
                        { ".json", "application/json" },
                        { ".jpg", "image/jpeg" },
                        { ".png", "image/png" },
                        { ".gif", "image/gif" },
                        { ".config", "text/xml" },
                        { ".woff2", "application/font-woff2"},
                        { ".eot", "application/vnd.ms-fontobject" },
                        { ".svg", "image/svg+xml" },
                        { ".woff", "font/x-woff" },
                        { ".txt", "text/plain" },
                        { ".map", "text/plain" }
                    })
                });
            });

            //SignalR托管
            app.Map("/signalr", map =>
            {
                // Setup the cors middleware to run before SignalR.
                // By default this will allow all origins. You can 
                // configure the set of origins and/or http verbs by
                // providing a cors options with a different policy.
                map.UseCors(CorsOptions.AllowAll);

                var hubConfiguration = new HubConfiguration
                {
                    //Resolver = **,
                    // You can enable JSONP by uncommenting line below.
                    // JSONP requests are insecure but some older browsers (and some
                    // versions of IE) require JSONP to work cross domain
                    EnableJSONP = true,
                    EnableDetailedErrors = true
                };                

                // Run the SignalR pipeline. We're not using MapSignalR
                // since this branch is already runs under the "/signalr"
                // path.
                map.RunSignalR(hubConfiguration);
            });

            //该值表示连接在超时之前保持打开状态的时间长度。
            //默认为110秒
            //GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110);

            //该值表示在连接停止之后引发断开连接事件之前要等待的时间长度。
            //默认为30秒
            GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(180);  //三分钟

            //用于表示两次发送保持活动消息之间的时间。如果启用，此值必须至少为两秒。设置为 null 可禁用。
            //默认为10秒,设置DisconnectTimeout后默认为DisconnectTimeout的3分之一
            //GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);

            //设置消息缓冲区大小
            //GlobalHost.Configuration.DefaultMessageBufferSize = 500;
        }
    }
}
